package demo2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        Boss huhansasn = new Boss();
        Reception reception = new Reception();
        //看股票的同事
        StockObserver tongshi1 = new StockObserver("魏关姹",huhansasn);
        NBAObserver tongshi2 = new NBAObserver("易观察",huhansasn);
        StockObserver tongshi3 = new StockObserver("魏关姹",reception);
        NBAObserver tongshi4 = new NBAObserver("易观察",reception);

        reception.Attach(tongshi3);
        reception.Attach(tongshi4 );
        reception.setSubjectState("老板要回来了！");
        reception.Notify();


        huhansasn.Attach(tongshi1);
        huhansasn.Attach(tongshi2);

        huhansasn.setSubjectState("我胡汉三回来了！");
        huhansasn.Notify();
    }
}

//通知者接口
interface Subject{
    void Attach(Observer observer);//增加观察者
    void Detach(Observer observer);//移除观察者
    void Notify();//通知
    void setSubjectState(String subjectState);
    String getSubjectState();

}
//具体的通知者类可能是老板也可能是前台小姐姐

class Boss implements Subject{
    private List<Observer> observers = new ArrayList<Observer>();
    private String action;

    @Override
    public void Attach(Observer observer) {
        observers.add( observer);
    }

    @Override
    public void Detach(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void Notify() {
        for (Observer o:observers
             ) {
            o.Update();
        }
    }
    private String subjectState;

    public String getSubjectState() {
        return subjectState;
    }

    public void setSubjectState(String subjectState) {
        this.subjectState = subjectState;
    }
}
//前台类
class Reception implements Subject{
    private List<Observer> observers = new ArrayList<Observer>();
    private String action;

    @Override
    public void Attach(Observer observer) {
        observers.add( observer);
    }

    @Override
    public void Detach(Observer observer) {
        observers.remove(observer);
    }

    @Override
    public void Notify() {
        for (Observer o:observers
        ) {
            o.Update();
        }
    }
    private String subjectState;

    public String getSubjectState() {
        return subjectState;
    }

    public void setSubjectState(String subjectState) {
        this.subjectState = subjectState;
    }
}

abstract class Observer{
    protected String name;
    protected Subject sub;

    public Observer(String name, Subject sub) {
        this.name = name;
        this.sub = sub;
    }
    public abstract void Update();

}

//看股票的同事
class StockObserver extends Observer{
    public StockObserver(String name, Subject sub) {
        super(name, sub);
    }

    @Override
    public void Update() {
        System.out.println( sub.getSubjectState()+name+ " 关闭股票行情，继续工作");

    }
}
//看NBA的同事
class NBAObserver extends Observer{
    public NBAObserver(String name, Subject sub) {
        super(name, sub);
    }

    @Override
    public void Update() {
        System.out.println( sub.getSubjectState()+name+ " 关闭NBA直播，继续工作");

    }
}
